// pages/msg_staff_details/msg_staff_details.js
let app = getApp();
var lid = ''

Page({

    /**
     * 页面的初始数据
     */
    data: {
        table: [],
        array1: [{
                'LCODE': '1',
                'LNAME': '一车间'
            },
            {
                'LCODE': '2',
                'LNAME': '二车间'
            },
            {
                'LCODE': '3',
                'LNAME': '三车间'
            },
            {
                'LCODE': '4',
                'LNAME': '四车间'
            },
            {
                'LCODE': '5',
                'LNAME': '五车间'
            },
        ],
    },
    switch1Change: function (e) {
        this.data.table[0].LISLIZHI = e.detail.value
        this.setData({
            table: this.data.table
        })
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.table[0].LUSERNAME = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.table[0].LDEPARTMENTID = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.table[0].LMOBILE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.table[0].LREMARK = e.detail.value
            this.setData({
                table: this.data.table
            })
        }
    },
    bindPickerChange1(e) { // 车间
        this.setData({
            index1: e.detail.value,
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.data.table[0].LFARM = this.data.array1[i].LCODE
                this.setData({
                    table: this.data.table
                })
            }
        }
    },
    bindPickerChange2(e) { // 车间
        this.setData({
            index2: e.detail.value,
        })
        for (var i = 0; i < this.data.array2.length; i++) {
            if (i == e.detail.value) {
                this.data.table[0].LROLENAME = this.data.array2[i].LROLENAME
                this.data.table[0].LROLEID = this.data.array2[i].LROLECODE
                this.setData({
                    table: this.data.table
                })
            }
        }
    },
    update(e) {
        let that = this
        let table = {
            'table': that.data.table
        }
        wx.request({
            url: 'https://' + app.globalData.url + '/staff/updateStaff',
            data: {
                "model": JSON.stringify(table),
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.success == true) {
                    wx.navigateBack({
                        delta: 1
                    })
                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: '更新失败',
                        icon: 'noen',
                        duration: 2000
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        lid = options.lid
        console.log(lid)

        function aa() {
            wx.request({
                url: 'https://' + app.globalData.url + '/Process/findStaffRole',
                data: {
                    LID: lid
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/json;charset=utf-8'
                },
                success: function (res) {
                    if (res.data.success == true) {
                        that.setData({
                            array2: res.data.obj
                        })
                        for (var i = 0; i < that.data.array2.length; i++) {
                            if (that.data.table[0].LROLENAME == that.data.array2[i].LROLENAME) {
                                that.setData({
                                    index2: i
                                })
                            }
                        }
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '获取数据失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {

                                }
                            }
                        })
                    }
                },
                fail: function (res) {

                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                },
            })
        }
        wx.request({
            url: 'https://' + app.globalData.url + '/staff/findStaffByLID',
            data: {
                LID: lid
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/json;charset=utf-8'
            },
            success: function (res) {
                if (res.data.success == true) {
                    that.setData({
                        table: res.data.obj
                    })
                    for (var i = 0; i < that.data.array1.length; i++) {
                        if (that.data.table[0].LFARM == that.data.array1[i].LCODE) {
                            that.setData({
                                index1: i
                            })
                        }
                    }
                    aa()
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '获取数据失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            },
            fail: function (res) {

                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})